<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>if (expression)</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>if (expression)</h1>

<p>Specifies the command(s) to perform if an <a href="../Variables.htm#Expressions">expression</a> evaluates to TRUE.</p>

<pre class="Syntax">if (expression)</pre>
<h3>Remarks</h3>
<p>An if-statement that contains an expression is differentiated from a <a href="IfEqual.htm">traditional-if</a> such as <code>If FoundColor &lt;&gt; Blue</code> by making the character after the word &quot;if&quot; an open-parenthesis. Although this is usually accomplished by enclosing the entire expression in parentheses, it can also be done with something like <code>if (x &gt; 0) and (y &gt; 0)</code>. In addition, the open-parenthesis may be omitted entirely if the first item after the word &quot;if&quot; is a <a href="../Functions.htm">function call</a> or an operator such as &quot;not&quot; or &quot;!&quot;.</p>
<p>If the if-statement's expression evaluates to true (which is any result other than an empty string or the number 0), the line or <a href="Block.htm">block</a> underneath it is executed. Otherwise, if there is a corresponding ELSE, execution jumps to the line or block underneath it.</p>
<p>When an IF or an <a href="Else.htm">ELSE</a> owns more than one line, those lines must be enclosed in <a href="Block.htm">braces</a>. However, if only one line belongs to an IF or ELSE, the braces are optional. See the examples at the bottom of this page.</p>
<p><a name="otb"></a>The One True Brace (OTB) style may optionally be used with if-statements that are expressions (but not <a href="IfEqual.htm">traditional if-statements</a>). For example:</p>
<pre>if (x &lt; y) <strong>{</strong>
    ...
<strong>}</strong>
if WinExist(&quot;Untitled - Notepad&quot;) <strong>{</strong>
    WinActivate
<strong>}</strong>
if IsDone <strong>{</strong>
    ...
<strong>}</strong> else <strong>{</strong>
    ...
<strong>}</strong></pre>
<p>Unlike an &quot;else&quot; statement -- which supports any type of statement immediately to its right -- an if-statement supports only a &quot;{&quot; to its right.</p>
<p>On a related note, the command <a href="IfBetween.htm">if var [not] between LowerBound and UpperBound</a> checks whether a variable is between two values, and <a href="IfIn.htm">if var [not] in value1,value2</a> can be used to check whether a variable's contents exist within a list of values.</p>
<h3>Related</h3>
<p><a href="../Variables.htm#Expressions">Expressions</a>, <a href="SetExpression.htm">Assign expression (:=)</a>, <a href="IfIn.htm">if var in/contains MatchList</a>, <a href="IfBetween.htm">if var between</a>, <a href="IfInString.htm">IfInString</a>, <a href="Block.htm">Blocks</a>, <a href="Else.htm">Else</a>, <a href="While.htm">While-loop</a></p>
<h3>Example</h3>
<pre class="NoIndent">if (A_Index &gt; 100 or Done)
    return

if (A_TickCount - StartTime &gt; 2*MaxTime + 100)
{
    MsgBox Too much time has passed.
    ExitApp
}

if (Color = &quot;Blue&quot; or Color = &quot;White&quot;)
{
    MsgBox The color is one of the allowed values.
    ExitApp
}
else if (Color = &quot;Silver&quot;)
{
    MsgBox Silver is not an allowed color.
    return
}
else
{
    MsgBox This color is not recognized.
    ExitApp
}</pre>

</body>
</html>
